<?php
class PreferenceWidget extends BaseWidget
{
    public function run()
    {
        // get search purpose id
        $userId = FacebookUtility::getUserId();
        $criteria = new CDbCriteria();
        $criteria->select = 'search_purpose_id';
        $search_purpose_id = UserModel::model()->findByPk($userId, $criteria)->search_purpose_id;

        // get preference
        $curPage = Yii::app()->controller->id.'/'.Yii::app()->controller->action->id;
        if($curPage == 'snapshot/index')
        {
            $preference = SnapshotPreferenceModel::model()->findByPk($userId);
            $ajaxUrl = 'ajax/updateSnapshotPreference';
        }
        elseif($curPage == 'search/index')
        {
            $preference = SearchPreferenceModel::model()->findByPk($userId);
            $ajaxUrl = 'ajax/search';
        }
        else
        {
            throw new Exception();
        }

        // params
        $params = array();
        $params['preference'] = $preference->getAttributes();
        $params['search_purpose_id'] = $search_purpose_id;
        $params['ajax_url'] = $ajaxUrl;

        // render
        $js = array('libs/zebra_transform-2.4.1.js');
        $css = array('libs/zebra_transform/zebra_transform-2.4.1.css');
        $this->render('preference', $params, $js, $css);
    }
}